package cn.elead.chaos.remote.web.system.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.remote.web.system.service.IRemoteDictDataService;
import cn.hutool.core.util.StrUtil;
import cn.elead.chaos.remote.web.system.domain.SysDictData;

/**
 * 字典数据Controller
 * 
 * @author luopeng
 * @date 2020-02-26 15:15:19
 */
@RestController
@RequestMapping("/remote/system/dict/data")
public class RemoteDictDataController extends BaseController
{
    @Autowired
    private IRemoteDictDataService dictDataService;

    /**
     * 查询字典数据列表
     */
    @GetMapping("/page")
    @RequiresPermissions("system:dict:list")
    public TableDataInfo<SysDictData> page(SysDictData sysDictData) {
        return dictDataService.page(sysDictData);
    }
    

    /**
     * 导出字典数据列表
     */
    @GetMapping("/export")
    @RequiresPermissions("system:dict:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    public String export(SysDictData sysDictData) {
        List<SysDictData> list = dictDataService.list(sysDictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        return util.exportExcel(list, "data");
    }

    /**
     * 获取字典数据详细信息
     */
    @GetMapping(value = "/{dictCode}") 
    @RequiresPermissions("system:dict:list")
    public SysDictData getInfo(@PathVariable("dictCode") Long dictCode) {
       	return dictDataService.getInfo(dictCode);
    }
    
    /**
	 * 根据字典类型查询字典数据信息
	 */
	@GetMapping(value = "/dictType/{dictType}")
	public List<SysDictData> dictType(@PathVariable String dictType) {
		return dictDataService.dictType(dictType);
	}

    /**
     * 新增字典数据
     */
    @PostMapping
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    public int add(@RequestBody SysDictData sysDictData) {
    	return dictDataService.add(sysDictData);
    }

    /**
     * 修改字典数据
     */
    @PutMapping
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    public int edit(@RequestBody SysDictData sysDictData) {
        return dictDataService.edit(sysDictData);
    }

    /**
     * 删除字典数据
     */
	@DeleteMapping("/{dictcodes}")
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    public int remove(@PathVariable Long[] dictCodes){
		String ids = StringUtils.join(dictCodes, StrUtil.COMMA);
        return dictDataService.remove(ids);
    }
}